package com.homework.controller;

import homework.facade.dto.Response;
import org.apache.commons.mail.Email;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;
import org.apache.commons.mail.SimpleEmail;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/email")
public class EmailController {

    @RequestMapping(value = "/{email}/{code}",method = RequestMethod.GET)
    public Response sendEmail (@PathVariable String email,@PathVariable String code) {
        Response response = new Response();
        Email emailInstance = new SimpleEmail();//创建一个HtmlEmail实例对象
        try {
            emailInstance.setHostName("smtp.126.com");//邮箱的SMTP服务器，一般123邮箱的是smtp.123.com,qq邮箱为smtp.qq.com
            emailInstance.setAuthentication("", "");//设置发送人到的邮箱和用户名和授权码(授权码是自己设置的)
            //由于邮件滥发等原因阿里云服务器禁用了25端口，所以这里得使用ssl加密传输（这样使用的端口号是465）
//            emailInstance.setSSLOnConnect(true);

            emailInstance.setCharset("utf-8");//设置发送的字符类型
            emailInstance.setFrom("");//发送人的邮箱为自己的，
            emailInstance.addTo(email);//设置收件人
            emailInstance.setSubject("测试验证码");//设置发送主题
            emailInstance.setMsg("你的验证码为:" + code);//设置发送内容
            emailInstance.send(); //发送邮件
        } catch (Exception e) {
            e.printStackTrace();
        }
        return response;
    }
}
